package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.dao.SeckillOrderMapper;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.service.seckill.SeckillOrdersService;
import com.qingcheng.task.MultiThreadingCreateOrder;
import com.qingcheng.util.IdWorker;
import com.qingcheng.pojo.seckill.SeckillStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;

@Service
public class SeckillOrdersServiceImpl implements SeckillOrdersService {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MultiThreadingCreateOrder multiThreadingCreateOrder;

    /**
     * 添加秒杀订单
     *
     * @param id       商品Id
     * @param time     商品秒杀开始时间
     * @param username 用户登录名
     * @return
     */
    @Override
    public boolean add(long id, String time, String username) {

        //自增特性，increment：让指定key的值自增value->返回自增的->单线程操作
        //递增，判断是否排队
        Long userQueueCount = redisTemplate.boundHashOps("UserQueueCount").increment(username, 1);
        if (userQueueCount > 1) {
            // 100: 表示有重复抢单
            System.out.println("重复下单");
            throw new RuntimeException("100");
        }

        //减少无效排队
        Long size = redisTemplate.boundListOps("SeckillGoodsCountList_" + time + "_" + id).size();
        if(size<=0){
            //101 :表示没有库存
            throw new RuntimeException("101");
        }

        //排队信息封装
        SeckillStatus seckillStatus = new SeckillStatus(username, new Date(), 1, id, time);

        //将秒杀抢单信息存入到Redis中，这里采用List存储，List本身是一个队列
        redisTemplate.boundListOps("SeckillOrderQueue").leftPush(seckillStatus);

        //将抢单状态存入到redis
        redisTemplate.boundHashOps("UserQueueStatus").put(username, seckillStatus);

        //异步操作
        multiThreadingCreateOrder.createOrder();

        return true;
    }

    /**
     * 查询用户的抢单状态
     *
     * @param username
     * @return
     */
    @Override
    public SeckillStatus queryStatus(String username) {
        return (SeckillStatus) redisTemplate.boundHashOps("UserQueueStatus").get(username);
    }

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    /**
     * 更新订单状态
     * @param outTradeNo 订单id
     * @param transactionId 支付流水号
     * @param username 用户名
     */
    public void updatePayStatus(String outTradeNo,String transactionId,String username){
        //订单数据从Redis数据库查询出来
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.boundHashOps("SeckillOrder").get(username);

//        if(seckillOrder==null){
//            System.out.println("查询的数据为空");
//        }else{
//            System.out.println("不为空");
//        }

        //修改状态
        seckillOrder.setStatus("1");//已支付

        //支付时间
        seckillOrder.setPayTime(new Date());

        //同步到mysql中
        seckillOrderMapper.insertSelective(seckillOrder);

        //清理redis中的订单
        redisTemplate.boundHashOps("SeckillOrder").delete(username);

        //清理排队标识
        redisTemplate.boundHashOps("UserQueueCount").delete(username);

        //清理抢单标识
        redisTemplate.boundHashOps("UserQueueStatus").delete(username);

    }


    /**
     * 根据用户名查询用户秒杀订单信息
     * @param username
     * @return
     */
    public SeckillOrder queryByUsername(String username){
        return (SeckillOrder) redisTemplate.boundHashOps("SeckillOrder").get(username);
    }
}
